package com.rfid.assistant.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.king.zxing.CameraScan;
import com.rfid.assistant.QrScanActivity;
import com.rfid.assistant.R;
import com.rfid.assistant.activity.adapter.MainAdapter;
import com.rfid.assistant.activity.adapter.OnItemClickListener;
import com.rfid.assistant.activity.presenter.MainPresenter;
import com.rfid.assistant.activity.view.IMainView;
import com.rfid.assistant.bean.Machine;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BluetoothMainActivity extends AppCompatActivity implements IMainView, OnItemClickListener {

    MainPresenter mPresenter;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    MainAdapter mAdapter;
    private List<Machine> mMachines;

    public static void startActivity(Context context,boolean send) {
        Intent intent = new Intent();
        intent.putExtra("send",send);
        intent.setClass(context, BluetoothMainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_main);
        ButterKnife.bind(this);
        mPresenter = new MainPresenter(this);
        mMachines = new ArrayList<>();
        mMachines.addAll(mPresenter.getMachines());
        mAdapter = new MainAdapter(mMachines);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.qr) {
            QrScanActivity.start(this);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100 && resultCode == Activity.RESULT_OK) {
            String txt = data.getStringExtra(CameraScan.SCAN_RESULT);
            doBluetoothConnect(txt);
        }
    }

    private void doBluetoothConnect(String mac) {
        System.out.println("connect mac = " + mac);
        mPresenter.connectAndAddMachine(mac);
    }

    @Override
    public void onConnect(String name, String mac) {
        Machine machine = new Machine();
        machine.setMac(mac);
        machine.setName(name);
        Toast.makeText(this,"连接: + " + name,Toast.LENGTH_SHORT).show();
        if(mMachines.contains(machine)) {

        } else {
            mPresenter.saveDoor(machine);
            mMachines.add(machine);
            mAdapter.notifyDataSetChanged();
        }
        BluetoothHomeActivity.startActivity(this);
    }

    @Override
    public void onLongClick(int pos) {
        Machine m = mMachines.remove(pos);
        mPresenter.deleteMachine(m);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onConnectFail() {
        Toast.makeText(this,"连接失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showConnecting() {
        Toast.makeText(this,"正在连接",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void hideConnecting() {
//        Toast.makeText(this,"连接结束",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(int pos) {
        mPresenter.connectAndAddMachine(mMachines.get(pos).getMac());
    }

    @Override
    public void onEdit(int pos) {
        EditText editText = new EditText(this);
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("修改名称")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String txt = editText.getText().toString();
                        mMachines.get(pos).setName(txt);
                        mAdapter.notifyDataSetChanged();
                        mPresenter.updateMachine( mMachines.get(pos));
                    }
                }).create();
        editText.setText(mMachines.get(pos).getName());
        dialog.setView(editText);
        dialog.show();
    }
}